using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PositionUtils
{
    /// <summary>
    /// 世界转换为屏幕坐标
    /// </summary>
    /// <param name="worldPoint"></param>
    /// <returns></returns>
    public static Vector2 WorldPointToScreenPoint(Vector3 worldPoint) {
        Vector2 screenPoint = Camera.main.WorldToScreenPoint(worldPoint);
        return screenPoint;
    }

    /// <summary>
    /// 屏幕坐标转换为世界坐标
    /// </summary>
    /// <param name="screenPoint"></param>
    /// <param name="planeZ"></param>
    /// <returns></returns>
    public static Vector3 ScreenPointToWorldPoint(Vector2 screenPoint,float planeZ) {
        Vector3 position = new Vector3(screenPoint.x,screenPoint.y,planeZ);
        Vector3 worldPoint = Camera.main.ScreenToWorldPoint(position);
        return worldPoint;
    }

    /// <summary>
    /// canvasMode为 ScreenSpaceCamera,WorldSpace是后传递参数 canvas.worldCamera,Overlay 传递参数null
    /// UI坐标转换为屏幕坐标
    /// </summary>
    /// <param name="uiCamera"></param>
    /// <param name="worldPoint"></param>
    /// <returns></returns>
    public static Vector2 UIPointToScreenPoint(Camera uiCamera,Vector3 worldPoint) {
        Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(uiCamera,worldPoint);
        return screenPoint;
    }

    /// <summary>
    /// 屏幕坐标转换为UGUI坐标
    /// </summary>
    /// <param name="uiCamera"></param>
    /// <param name="rt"></param>
    /// <param name="screenPoint"></param>
    /// <returns></returns>
    public static Vector3 ScreenPointToUIPoint(Camera uiCamera,RectTransform rt,Vector2 screenPoint) {
        Vector3 globalMousePos;
        RectTransformUtility.ScreenPointToWorldPointInRectangle(rt,screenPoint,uiCamera,out globalMousePos);
        return globalMousePos;
    }

    public static Vector2 ScreenPointToUILocationPoint(Camera uiCamera,RectTransform parentRT,Vector2 screenPoint) {
        Vector2 localPos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRT,screenPoint,uiCamera,out localPos);

        return localPos;
    }

    public static Vector3 twoSem(Vector3 vec) {
        return new Vector3(
            Mathf.Floor(vec.x * 10f) / 10f,
            Mathf.Floor(vec.y * 10f) / 10f,
            Mathf.Floor(vec.z * 10f) / 10f
        );
    }


}
